Skip to main content

Sending ETH in Batch with Axir Smart Account

Overview

This guide covers how to send ETH to multiple recipients in a single transaction.

Prerequisites

import { AxirCore } from "axr-erc4337-sdk";
import { ethers } from "ethers";
import { type Hex, type Address } from "viem";

Implementation

1. Initialize AxirCore

const axirCore = new AxirCore(
process.env.PRIVATE_KEY as Hex,
process.env.RPC_URL as string,
process.env.BUNDLER_URL as string,
BigInt(0), // nonce
"baseSepolia" // network name
);

2. Prepare and Send Batch ETH Transfers

// Prepare batch ETH transfer UserOperation
const batchUserOp = await axirCore.prepareUserOperation(
[
{
contract: receiver1Address,
value: ethers.parseEther("0.001"),
abi: [], // Empty for ETH transfers
functionName: "",
args: [],
},
{
contract: receiver2Address,
value: ethers.parseEther("0.002"),
abi: [],
functionName: "",
args: [],
},
],
undefined,
usePaymaster,
paymasterType
);

// Estimate gas costs
const gasEstimate = await axirCore.estimateUserOperationGas(
batchUserOp,
usePaymaster,
paymasterType
);

// Log gas estimates
console.log("Gas Estimation for batch ETH transfer:");
if (gasEstimate.totalGasFeeInEth) {
console.log(
`Estimated gas fee in ETH: ${ethers.formatEther(
gasEstimate.totalGasFeeInEth
)} ETH`
);
}
if (gasEstimate.totalGasFeeInToken) {
console.log(
`Estimated gas fee in tokens: ${ethers.formatUnits(
gasEstimate.totalGasFeeInToken,
gasEstimate.tokenDecimals
)} ${gasEstimate.tokenSymbol}`
);
}

// Execute the batch transaction
const txHash = await axirCore.executeUserOperation(batchUserOp);
console.log("Batch transaction hash:", txHash);